package com.cpecc.dao.wuzi;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.ShenQing;
import com.cpecc.model.wuzi.ShenQingJL;

@Repository
public class ShenQingJLDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void save(ShenQingJL shenQingJL){
		Session session=sessionFactory.getCurrentSession();
		session.save(shenQingJL);
		session.flush();
	}
	
	@SuppressWarnings("unchecked")
	public List<ShenQingJL> getAllJL(String shenQingId){
		String hql="from ShenQingJL t where t.shenQing.id=? order by t.caoZuoRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, shenQingId);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<ShenQing> getMySPJL(User user){
		String hql="select t.shenQing from ShenQingJL t where t.user.id=? group by t.shenQing order by t.shenQing.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<User> listBuMenSP(String shenQingId){
		String hql="select t.user from ShenQingJL t where t.shenQing.id=? and t.huanJie.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, shenQingId);
		query.setInteger(1, 102);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getWeiJieShou(Date date){
		String hql="select t.shenQing.id from ShenQingJL t where t.caoZuoRQ<? and t.caoZuoRQ in(select max(u.caoZuoRQ) from ShenQingJL u where u.shenQing.huanJie.id=107 group by u.shenQing)";
		Session session=sessionFactory.openSession();
		Query query=session.createQuery(hql);
		query.setParameter(0, date);
		List<String> shenQingIds=query.list();
		session.close();
		return shenQingIds;
	}
	
	public void save1(ShenQingJL shenQingJL){
		Session session=sessionFactory.openSession();
		session.save(shenQingJL);
		session.flush();
		session.close();
	}
}
